// standard libraries
#include <string>

// custom libraries
#include "..\..\include\graphics\graphics_renderer.h"
#include "..\..\include\graphics\font.h"

#include "..\..\include\game_manager.h"
#include "..\..\include\resource_manager.h"
#include "..\..\include\settings.h"
#include "..\..\include\user_controller.h"

#include "..\..\include\game_state\end_state.h"
#include "..\..\include\game_state\exit_state.h" // matching header


// ==================================
//     SINGLETON IMPLEMENTATION
// ==================================

ExitState* ExitState::Instance()
{
	// Meyers singleton
	static ExitState obj;
	return &obj;
}

ExitState::ExitState() 
{
	isOpened_ = false;
	textFont_ = 0; // Will be loaded in ExitState::onOpen()
}

ExitState::~ExitState()
{
	if (isOpened_)
		onClose();
}

ExitState::ExitState(const ExitState& other) {}
ExitState& ExitState::operator=(const ExitState& other) { return *this; } 


// ==================================
//          OPEN and CLOSE
// ==================================

void ExitState::onOpen()
{
	ResourceManager* rm = ResourceManager::Instance();
	Settings* set = Settings::Instance();

	std::string fontPath = set->getFontFilepath();
	textFont_ = rm->getFont(fontPath);

	isOpened_ = true;
}

void ExitState::onClose()
{
	ResourceManager::Instance()->release(textFont_); 
	textFont_ = 0;
	
	isOpened_ = false;
}


// ==================================
//          ENTER and EXIT
// ==================================

void ExitState::onEnter()
{
	currentSelection_ = NO;
}

void ExitState::onExit() {}


// ==================================
//         UPDATE and RENDER
// ==================================

void ExitState::update(GameManager* game, UserController* userController)
{
	userController->scanButtons();

	if (userController->isButtonPressed(UserController::SELECT)) {
		if (currentSelection_ == YES)
			game->changeState(EndState::Instance());
		else
			game->popState();
	} else if (userController->isButtonPressed(UserController::EXIT)) {
		game->popState();
	} else if (userController->isButtonPressed(UserController::LEFT)) {
		currentSelection_ = NO;
	} else if (userController->isButtonPressed(UserController::RIGHT)) {
		currentSelection_ = YES;
	}
}

void ExitState::render(GraphicsRenderer* graphicsRenderer)
{
	int textSize = 12;

	std::string question  = "Are you sure you want to quit?";
	std::string answerNo  = "No"; 
	std::string answerYes = "Yes";

	unsigned int rgbaBackground  = 0x000000FF; // Black
	unsigned int rgbaQuestion    = 0xFFFFFFFF; // White
	unsigned int rgbaSelected    = 0xFFFFFFFF; // White
	unsigned int rgbaNotSelected = 0x808080FF; // Gray


	// Background
	graphicsRenderer->fillScreen(rgbaBackground);
	
	// Question
	graphicsRenderer->printText(question, 200, 240, 
		*textFont_, textSize, rgbaQuestion);

	// No
	unsigned int color = rgbaNotSelected;
	if (currentSelection_ == NO)
		color = rgbaSelected;
	graphicsRenderer->printText(answerNo, 240, 260,
		*textFont_, textSize, color);
	
	// Yes
	color = rgbaNotSelected;
	if (currentSelection_ == YES)
		color = rgbaSelected;
	graphicsRenderer->printText(answerYes, 370, 260,
		*textFont_, textSize, color);

	// Render
	graphicsRenderer->render();
}
